﻿package{
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;	
	
	public class SimpleLoader {
		private var urlReq:URLRequest;
		private var back:Function;
		private var loader;
		var gut,url;
		function SimpleLoader(url:String,type = null){
			this.url = url;
			if(type == null){
				type  = getType(url);
			}
			if(type == 'pic'){
				loader = new Loader();
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE,function(e){reply(loader,loader.content,true)});
				loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,errorTip);
			}else{
				loader = new URLLoader();
				loader.dataFormat = URLLoaderDataFormat.TEXT;
				loader.addEventListener(Event.COMPLETE,function(e){reply(loader,loader.data,false)});
				loader.addEventListener(IOErrorEvent.IO_ERROR,errorTip);
				loader.dataFormat = type;
			}
		}
		function set callback(back:Function){
			var req = new URLRequest();
			req.url = url;
			loader.load(req);
			this.back = back;
		}		
		private function reply(ldr,content,flag:Boolean){
			if(flag){
				ldr.unload();
			}
			gut = content
			back();
		}
		private function getType(url:String){
			switch(url.split('.').pop()){
				case 'swf':
				case 'jpg':
				case 'gif':
				case 'png':
					return 'pic'
				case 'txt':
					return URLLoaderDataFormat.TEXT;
				default:
					return URLLoaderDataFormat.BINARY;
			}
		}
		private function errorTip(e:Event){
			trace(url);
		}
	}
}